﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SGGWElevator
{
    public delegate void FloorSelectedEventHandler(object sender, Floor selectedFloor);

    /// <summary>
    /// Interaction logic for FloorButtonsPanel.xaml
    /// Author: Michał Szczepek
    /// </summary>
    public partial class FloorButtonsPanel : UserControl
    {
        /// <summary>
        /// Contains floor definition for buttons.
        /// </summary>
        BiDictionary<Button, Floor> _dictButtonsFloors;

        /// <summary>
        /// Event called whenever floor
        /// </summary>
        public event FloorSelectedEventHandler FloorSelectedEvent;


        public FloorButtonsPanel()
        {
            InitializeComponent();

            _dictButtonsFloors = new BiDictionary<Button, Floor>();
            _dictButtonsFloors.Add(MinusOneFloorButton, Floor.MinusOne);
            _dictButtonsFloors.Add(LobbyButton, Floor.Lobby);
            _dictButtonsFloors.Add(FirstFloorButton, Floor.One);
            _dictButtonsFloors.Add(SecondFloorButton, Floor.Two);
            _dictButtonsFloors.Add(ThirdFloorButton, Floor.Three);
        }


        /// <summary>
        /// Handles buttons click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnButtonClick(object sender, RoutedEventArgs args)
        {
            Button clickedButton = (Button)sender;

            // Get selected floor
            Floor selectedFloor = _dictButtonsFloors.GetByFirst(clickedButton);

            // Call an event
            if (FloorSelectedEvent != null)
                FloorSelectedEvent(this, selectedFloor);
        }

        /// <summary>
        /// Changes floor buttons image to lighted up.
        /// </summary>
        public void SwitchOnOffFloor(Floor floor, bool active)
        {
            Button btn = _dictButtonsFloors.GetBySecond(floor);

            // Check is btn not null
            if (btn == null)
                return;

            if (active)
                btn.Foreground = this.Resources["ActiveForeground"] as Brush;
            else
                btn.Foreground = this.Resources["DefaultForeground"] as Brush;
        }
    }
}
